package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.common.utils.string.StringUtil;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.TempPayStatisFacade;
import com.qding.park.manager.pojo.enums.OrderSourceEnum;
import com.qding.park.manager.pojo.example.NcTempPayExample;
import com.qding.park.manager.pojo.noncore.NcTempPayInfo;
import com.qding.park.manager.service.statis.mapper.TbReserveStatisMapper;

/**
 * @ClassName: TempPayStatisFacadeImpl
 * @Description: 临停缴费单统计查询
 * @author Comsys-Administrator
 * @date 2016年10月12日 上午10:36:20
 *
 */
@Service("tempPayStatisFacade")
public class TempPayStatisFacadeImpl implements TempPayStatisFacade {
    private static final Logger _log = LoggerFactory.getLogger(TempPayStatisFacadeImpl.class);
	@Autowired
	private TbReserveStatisMapper tbReserveMapper;
	
	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public ParkDataResult list(NcTempPayExample example, int jumpPage, int pageLength) throws BaseException {

		String tablename_tempPay = MongodbConstant.TABLENAME_TEMPPAY;

		Criteria matchCrieria = new Criteria();
		matchCrieria.where("_id").not();
		
		if (!StringUtils.isBlank(example.getCarNum())) {
//			matchCrieria.and("carNum").is(example.getCarNum());
			matchCrieria.and("carNum").regex(".*"+example.getCarNum()+".*");
		}
		
//		if (!StringUtils.isBlank(example.getParkId())) {
//			matchCrieria.and("parkId").is(Integer.valueOf(example.getParkId()));
//		}
		if (!StringUtils.isBlank(example.getParkId())&&!ListUtil.isEmpty(example.getInParks())) {
			matchCrieria.andOperator(Criteria.where("parkId").is(Integer.valueOf(example.getParkId())),
					Criteria.where("parkId").in(example.getInParks()));
		}else if(!StringUtils.isBlank(example.getParkId())){
			matchCrieria.and("parkId").is(Integer.valueOf(example.getParkId()));
		}else if(!ListUtil.isEmpty(example.getInParks())){
			matchCrieria.and("parkId").in(example.getInParks());
		}
		
		Query query=Query.query(matchCrieria);
		query.with(new Sort(new Order(Direction.DESC,"create_date")));
		query.skip((jumpPage - 1) * pageLength);
		query.limit(pageLength);
		
		Query querytotal=Query.query(matchCrieria);
		
		List<NcTempPayInfo> ncTempPayInfos= mongoTemplate.find(query, NcTempPayInfo.class, tablename_tempPay);
		
		long total= mongoTemplate.count(querytotal, tablename_tempPay);
		
		List dataList = new ArrayList();
		int totalFee=0;
		for(NcTempPayInfo ncTempPayInfo:ncTempPayInfos){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderId", ncTempPayInfo.getPayOrderID());
			if(ncTempPayInfo.getOrderSource()==OrderSourceEnum.OUTLINE.getType()){
				map.put("orderStatus", ncTempPayInfo.getOutTime()==null?"成功":(DateUtils.formatDate(ncTempPayInfo.getOutTime(),DateUtils.DATE_FORMAT_DATETIME)+" 成功"));
			}else{
				map.put("orderStatus", ncTempPayInfo.getCreate_date()==null?"成功":(DateUtils.formatDate(ncTempPayInfo.getCreate_date(),DateUtils.DATE_FORMAT_DATETIME)+" 成功"));
			}
			
			map.put("tel", ncTempPayInfo.getTel());
			map.put("carNum", ncTempPayInfo.getCarNum());
			map.put("payType", ncTempPayInfo.getOrderSource());
			map.put("realFee", ncTempPayInfo.getFeeMoney());
//			map.put("receivableParkingFee", ncMonthRent.getShouldPaymentAmount());
			
			totalFee=totalFee+ncTempPayInfo.getFeeMoney();
			dataList.add(map);
		}
		
		
		ParkDataResult parkDataResult=ParkDataResult.createObjDate();
		parkDataResult.putDataOnObj("currentPage", jumpPage);
		parkDataResult.putDataOnObj("numPerPage", pageLength);
		parkDataResult.putDataOnObj("totalCount", total);
		parkDataResult.putDataOnObj("hast", jumpPage > 1 ? true : false);
		parkDataResult.putDataOnObj("next", jumpPage * pageLength < total ? true : false);
		parkDataResult.putDataOnObj("totalFee", totalFee);
		parkDataResult.putDataOnObj("list",dataList);
		return parkDataResult;
	}

	@Override
	public List<NcTempPayInfo> exportExcel(NcTempPayExample example) {
		String tablename_tempPay = MongodbConstant.TABLENAME_TEMPPAY;
		
		Criteria matchCrieria = new Criteria();
		matchCrieria.where("_id").not();
		
		if (example.getStartTime() != null && example.getEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("create_date").gte(example.getStartTime()),
					Criteria.where("create_date").lte(example.getEndTime()));
		} else if (example.getStartTime()!= null) {
			matchCrieria.andOperator(Criteria.where("create_date").gte(example.getStartTime()));
		} else if (example.getEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("create_date").lte(example.getEndTime()));
		}
		
//		if(!StringUtils.isBlank(example.getParkId())){
//			matchCrieria.and("parkId").is(Integer.valueOf(example.getParkId()));
//		}
		if (!StringUtils.isBlank(example.getParkId())&&!ListUtil.isEmpty(example.getInParks())) {
			matchCrieria.andOperator(Criteria.where("parkId").is(Integer.valueOf(example.getParkId())),
					Criteria.where("parkId").in(example.getInParks()));
		}else if(!StringUtils.isBlank(example.getParkId())){
			matchCrieria.and("parkId").is(Integer.valueOf(example.getParkId()));
		}else if(!ListUtil.isEmpty(example.getInParks())){
			matchCrieria.and("parkId").in(example.getInParks());
		}

		Query query=Query.query(matchCrieria);
		query.with(new Sort(new Order(Direction.DESC,"create_date")));
		
		List<NcTempPayInfo> ncTempPayInfos= mongoTemplate.find(query, NcTempPayInfo.class, tablename_tempPay);
		return ncTempPayInfos;
	}

	@Override
	public ParkDataResult orderInfoById(String orderId) throws BaseException {
		String tablename_tempPay = MongodbConstant.TABLENAME_TEMPPAY;
		Criteria matchCrieria = new Criteria();
		matchCrieria.and("payOrderID").is(orderId);
		Query query=Query.query(matchCrieria);
		NcTempPayInfo ncTempPayInfos= mongoTemplate.findOne(query, NcTempPayInfo.class, tablename_tempPay);
		Map<String, Object> map = new HashMap<String, Object>();
		ParkDataResult parkDataResult=ParkDataResult.createObjDate();
		if(ncTempPayInfos!=null){
			map.put("orderId", ncTempPayInfos.getPayOrderID());
			map.put("orderStatus", ncTempPayInfos.getCreate_date()==null?"成功":(DateUtils.formatDate(ncTempPayInfos.getCreate_date(),DateUtils.DATE_FORMAT_DATETIME)+" 成功"));
			map.put("tel", StringUtil.isBlank( ncTempPayInfos.getTel())?"":ncTempPayInfos.getTel());
			map.put("carNum", ncTempPayInfos.getCarNum());
			map.put("payType", ncTempPayInfos.getOrderSource());
			map.put("realFee", ncTempPayInfos.getFeeMoney());
			parkDataResult.putMapOnObj(map);
		}
		
		return parkDataResult;
	}

}
